<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        //函数
        function getNum(num1,num2){//形参
            var result = 0;
            for(var i = num1; i < num2; i++){
                result += i;
            }
            console.log(result);
        }
        getNum(10,100);//实参
        function cook(){
            console.log('a');
            /* cook(); */
        }
        cook();
        function getSum(num1,num2){
            console.log(num1+num2);
            return num1+num2;
        }
        console.log(getSum(1,2,3));
        getSum(1);//NaN
        function getSum(num1,num2){
            return num1>num2?num1:num2;
        }
        function getArrayMax(arr){
            var max = 0;
            for (var i = 0; i < arr.length; i++) {
                if(arr[i] > max){
                    max = arr[i];
                }
            }
            return max;//return后面的代码不会被执行
        }
        console.log(getArrayMax([1,2,3,4]));
        console.log();//return 1,2;   返回最后一个值 2
        //return [1,2,3,4,5,1]; 返回数组
        function fun1(){

        }
        console.log(fun1());//undefined
        //arguments
        function fun2(){
            console.log(arguments);//伪数组 ， 存储了传递过来的实参，没有常规的数组方法，只有函数有arguments对象，函数内置arguments对象
            for(var i = 0; i<arguments.length; i++){
                console.log(arguments[i]);
                
            }
        }
        fun2(1,1,1,1,3,3,4);

        function reverse(arr){//翻转reverse
            var newArray = [];
            for(var i = arr.length - 1; i >= 0; i--){
                newArray[newArray.length] = arr[i];
            }
            return newArray;
        }
        console.log(reverse([1,2,3,4,5,6,7]));
        console.log(reverse(['a','s','c']));
        function sort(arr){
            for(var i = 0; i < arr.length - 1; i++){
                for(var j = 0; j < arr.length - i - 1; j++){
                    var temp = 0;
                    if(arr[j+1] > arr[j]){
                        temp = arr[j+1];
                        arr[j+1] = arr[j];
                        arr[j] = temp; 
                    }
                }
            }
            return arr;
        }
        console.log(sort([1,2,3,4,5,6]));
        function isRunYear(year){
            return year % 4 == 0 && year % 100 != 0 || year % 400 ==0/* ?'闰年':'不是闰年' */;
        }
        console.log(isRunYear(1999));
        function fun3(){
            console.log('fun3');
            fun4();
        }
        function fun4(){
            console.log('fun4');
            
        }
        function isRunYear2(year){
            return isRunYear(year)?'一年中的二月有29天':'一年中的二月有28天';
        }
        console.log(isRunYear2(2000));
        //函数声明
        var sun = function(str){
            console.log(str);
            
        }
        sun('sss');
        //作用域
        var num3 = 10;//全局变量 全局作用域 作用于整个script标签
        function run(str){//形参可以视为局部变量
            var num3 = 33;//在函数内部声明为局部作用域
            num5 = 44;//在函数内部没有声明直接赋值为全局变量
        }
        console.log(num5);
        //作用域链
        var num6 = 10;
        function rn(){
            var num6 = 20;
            function run(){
                console.log(num6);//20 就近原则
                
            }
        }
        //预解析   var 跟函数提升到当前作用域的最前面
        run2();//可以运行
        function run2(){
            console.log();
            
        }
        run3();//报错
        var run3 = function(){//函数表达式  变量 = 函数
            console.log();
            
        }
        run3();//不报错
        //案例
        run4();
        function run4(){
            console.log(num1);//undefined
            var num1 = 10;
            console.log(num1);10
            var num2 = 20;
            console.log(num2);//20
            
        }
        /* 预解析为： run4();
        function run4(){
            var num1;
            var num2;
            console.log(num1);//undefined
            num1 = 10;
            console.log(num1);10
            num2 = 20;
            console.log(num2);//20
            
        } */
        run5();
        console.log(b);//9
        console.log(c);//9
        console.log(a);//报错
        function run5(){
            var a = b = c = 9;
            console.log(a);
            console.log(b);
            console.log(c);
        }
        /* 预解析为
         function run5(){
             var a;
            a = b = c = 9;//b和c为全局变量
            console.log(a);
            console.log(b);
            console.log(c);
        }
        run5();
        console.log(b);
        console.log(c);
        console.log(a);
        */
    </script>
</head>
<body>
    
</body>
</html>